package me.hetong.caigou.ui.view;

import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
import me.hetong.caigou.Global;

import java.util.ArrayList;
import java.util.List;

import static me.hetong.caigou.Global.config;

/**
 * 左侧菜单
 *
 * @author hetong
 */
public class Left {

    private static Label click = new Label();
    private static final List<Label> labelList = new ArrayList<>();

    public static Pane init() {

        BorderPane borderPane = new BorderPane();
        borderPane.setStyle("-fx-background-color: linear-gradient(to right top, #d16ba5, #c777b9, #ba83ca, #aa8fd8, #9a9ae1, #8aa7ec, #79b3f4, #69bff8, #52cffe, #41dfff, #46eefa, #5ffbf1);");

        int leftWidth = config.getLeftWidth();
        int height = config.getHeight();

        borderPane.setPrefSize(leftWidth, height);
        borderPane.setMinSize(leftWidth, height);
        borderPane.setMaxSize(leftWidth, height);

        borderPane.setTop(createTop());
        borderPane.setCenter(createCenter());
        borderPane.setBottom(createBottom());

        return borderPane;
    }

    private static VBox createCenter() {
        VBox vBox = new VBox();
        vBox.setAlignment(Pos.TOP_CENTER);

        vBox.getChildren().add(createLabel(" 棋局推演", "#ip"));
        createLabel(" 棋谱管理", "#pu");// 只初始化不显示
//        createLabel(" 编辑局面", "#ecp");// 只初始化不显示
//        vBox.getChildren().add(createLabel(" 高级设置"));
        vBox.getChildren().add(createLabel(" 使用帮助", "#aup"));
        vBox.getChildren().add(createLabel(" 关闭程序", event -> {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setTitle("操作确认");
            alert.setHeaderText("关闭程序");
            alert.setContentText("关闭程序前将自动保存当前棋谱，您确认要关闭程序吗？");
            alert.showAndWait();
            if (alert.getResult() == ButtonType.OK) {
                Global.engine.shutdown();
                Global.saveWen();
                Platform.exit();
            }
        }));

        return vBox;
    }

    private static VBox createTop() {

        Image image = new Image("image/logo2.png");
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(80);
        imageView.setFitHeight(80);

//        Label label = new Label("菜狗象棋");
//        label.setStyle("-fx-font-size: 12;-fx-text-fill: #ffffff;-fx-font-weight: bolder;");

        VBox vBox = new VBox();
        vBox.setSpacing(config.getSpace());
        vBox.setAlignment(Pos.CENTER);
        vBox.setPadding(new Insets(30, 0, 30, 0));

        vBox.getChildren().add(imageView);
//        vBox.getChildren().add(label);
        return vBox;
    }

    private static Pane createBottom() {
        Label label = new Label("v2023.2.1");
        label.setStyle("-fx-text-fill: #ffffff");

        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);
        vBox.setPadding(new Insets(config.getPadding()));
        vBox.getChildren().add(label);
        return vBox;
    }

    private static Label createLabel(String text, EventHandler<MouseEvent> event) {
        return createLabel(text, null, null, event);
    }

    private static Label createLabel(String text) {
        return createLabel(text, null, null, null);
    }

    private static Label createLabel(String text, String id) {
        return createLabel(text, id, null, null);
    }

    public static void switchToId(String id) {
        for (Label label : labelList) {
            if (label.getId().equals(id)) {
                switchToId(label);
                return;
            }
        }
    }

    private static void switchToId(Label label) {

        /*
         如果点击菜单就是当前菜单则不做任何处理
         */
        if (click == label) {
            return;
        }

        /*
         去掉当前菜单的背景色
         */
        click.setStyle("-fx-background-color: transparent");

        for (Label li1 : labelList) {

            if (li1 == label) {

                label.setStyle("-fx-background-color: #8BA6EBaa");
                click = label;

                for (Pane li : Right.list) {
                    if (li.getId().equals(label.getId())) {
                        li.setVisible(true);
                        li.setManaged(true);
//                        new Bounce(label).play();
//                        new FadeIn(li).play();
                    } else {
                        if (li.isVisible() || li.isVisible()) {
                            li.setVisible(false);
                            li.setManaged(false);
                        }
                    }
                }
                return;
            }
        }
    }

    private static Label createLabel(String text, String id, String icon, EventHandler<MouseEvent> event) {
        Label label = new Label(text);
        label.setId(id);
        label.setOnMouseClicked(event);
//        if (StrUtil.isNotBlank(icon)) {
//            FontIcon fontIcon = new FontIcon(icon);
//            fontIcon.setIconColor(Paint.valueOf("#982378"));
//            fontIcon.setIconSize(12);
//            label.setGraphic(fontIcon);
//        }
        label.setPrefWidth(config.getLeftWidth());
        label.setAlignment(Pos.CENTER);
        label.setPadding(new Insets(10, 0, 10, 0));
        label.setTextFill(Paint.valueOf("#ffffff"));
        if (event != null) {
            label.setOnMouseClicked(event);
        } else {
            label.setOnMouseClicked(mouseEvent -> {
                switchToId(label);
            });
        }
        label.setOnMouseEntered(mouseEvent -> {
            label.setStyle("-fx-background-color: #8BA6EBaa");
        });
        label.setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (label != click) {
                    label.setStyle("-fx-background-color: transparent");
                }
            }
        });

        labelList.add(label);
        return label;
    }
}